﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SGRapidForm.Permission.Service.Interface;
using SGRapidForm.Common;
using SGRapidForm.Permission.Entity;
using System.Web.SessionState;

namespace SGDemo.Web.Handler
{
    /// <summary>
    /// HelpHandler 的摘要说明
    /// </summary>
    public class HelpHandler : IHttpHandler, IRequiresSessionState
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        public void ProcessRequest(HttpContext context)
        {
            if (string.IsNullOrEmpty(context.Request.QueryString["PageCode"]))
            {
                throw new ArgumentNullException("PageCode参数不存在");
            }

            PageEntity page =  _permissionService.Page_GetKeyidByPageCode(context.Request.QueryString["PageCode"]);
            string helpKeyid = page.HelpKeyid.Trim();
            if(!string.IsNullOrEmpty(helpKeyid))
            {
                string helpCategoryKeyid = _permissionService.Help_GetByKeyid(page.HelpKeyid).HelpCategoryKeyid;
                context.Response.Redirect(string.Format("~/Help/HelpItem.aspx?Keyid={0}&CategoryKeyid={1}", helpKeyid, helpCategoryKeyid), true);
            }
            context.Response.ContentType = "text/html";
            context.Response.Write("暂无相关帮助文档，如有问题可以联系管理员<a href=\"../ContactAdmin.html\">[查看联系方式]</a>");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}